(ns pe.p27
  (:use pe.primes))

(defn quadratic [a b n]
  (+ (* n n) (* a n) b))

(defn bind [a b] (partial quadratic a b))

(defn quad-sequence [f] (map first (iterate (fn [[val idx]] [(f (inc idx)) (inc idx)]) [(f 0) 0])))

(defn num-primes-in-sequence [a b]
  (count (take-while prime? (quad-sequence (bind a b)))))

(defn perm-sequence [arange brange]
  (for [a arange b brange] [a b]))

(defn variant-sequence []
  (for [[a b] (perm-sequence (range -999 1000) (range -999 1000))] [a b (num-primes-in-sequence a b)]))

(defn p27 []
  (let [val (reduce
              (fn [[a1 b1 num1] [a2 b2 num2]]
                (if (> num1 num2) [a1 b1 num1] [a2 b2 num2])) (variant-sequence))]
      (* (first val) (fnext val))))

(println (p27))